package com.leetcode.partition3;

/**
 * @author `RKC`
 * @date 2021/10/20 12:42
 */
public class LC278第一个错误的版本 {

    private static class VersionControl {

        public boolean isBadVersion(int version) {
            //自己指定错误的初始版本号
            return version >= 2;
        }
    }

    public static class Solution extends VersionControl {

        public int firstBadVersion(int n) {
            int left = 0, right = n;
            while (left <= right) {
                if (left == right) return left;
                int mid = (left + right) >>> 1;
                //如果当前版本是好的就往后查，如果当前版本是坏的就往前面查
                if (isBadVersion(mid)) {
                    right = mid;
                } else {
                    left = mid + 1;
                }
            }
            return -1;
        }
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.firstBadVersion(5));
    }
}
